local joy__shenfu = fk.CreateSkill {

  name = "joy__shenfu",

  tags = {  },

}



joy__shenfu:addEffect(fk.EventPhaseStart, {
  name = "joy__shenfu",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(joy__shenfu.name) and player.phase == Player.Finish
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    if player:getHandcardNum() % 2 == 1 then
      while true do
        local tos = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper), 1, 1, "#shenfu-damage", joy__shenfu.name, true)
        if #tos == 0 then break end
        n = n + 1
        local to = room:getPlayerById(tos[1])
        room:damage{
          from = player,
          to = to,
          damage = 1,
          damageType = fk.ThunderDamage,
          skillName = joy__shenfu.name,
        }
        if not to.dead then
          break
        end
      end
    else
      while true do
        local tos = room:askForChoosePlayers(player, table.map(table.filter(room.alive_players, function(p)
          return p:getMark("joy__shenfu-turn") == 0 end), Util.IdMapper), 1, 1, "#shenfu-hand", joy__shenfu.name, true)
        if #tos == 0 then break end
        n = n + 1
        local to = room:getPlayerById(tos[1])
        room:setPlayerMark(to, "joy__shenfu-turn", 1)
        if to:isKongcheng() then
          to:drawCards(1, joy__shenfu.name)
        else
          local choice = room:askForChoice(player, {"shenfu_draw", "shenfu_discard"}, joy__shenfu.name)
          if choice == "shenfu_draw" then
            to:drawCards(1, joy__shenfu.name)
          else
            local card = room:askForCardsChosen(player, to, 1, 1, "he", joy__shenfu.name)
            room:throwCard(card, joy__shenfu.name, to, player)
          end
        end
        if to:getHandcardNum() ~= to.hp then
          break
        end
      end
    end
    if not player.dead and n > 0 then
      player:drawCards(math.min(n, 5), joy__shenfu.name)
    end
  end,
})

return joy__shenfu